package com.test.springmybatis;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

public class SingleThreadParallelStreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        // 创建一个只包含一个线程的 ForkJoinPool
        ForkJoinPool singleThreadPool = new ForkJoinPool(10);

        // 使用 parallelStream 并指定单线程的 ForkJoinPool
        List<Integer> squaredNumbers = numbers.parallelStream()
                .sequential() // 确保在单线程中执行
                .map(num -> num * num)
                .collect(Collectors.toList());

        // 打印结果
        System.out.println("Squared Numbers: " + squaredNumbers);

        // 关闭线程池
        singleThreadPool.shutdown();
    }
}
